<template>
  <topCard queryName="角色名称" @SearchInput="getSearchInput" @reset="reset" />
  <el-card class="bottomCard" v-loading="loading">
    <el-button @click="addDrawer" type="primary">添加</el-button>
    <el-button
      @click="deleteSelectItem"
      type="danger"
      :disabled="!selectArr.length"
    >
      批量删除
    </el-button>
    <el-table
      class="bottom-table"
      border
      :data="roleList"
      @selection-change="selectChange"
    >
      <el-table-column align="center" type="selection" width="55" />

      <el-table-column label="角色名称" align="center">
        <template #default="scope">
          {{ scope.row.rolename }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template #default="scope">
          {{ formatDateTime(scope.row.createdAt) }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center">
        <template #default="scope">
          {{ formatDateTime(scope.row.updatedAt) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="260" align="center">
        <template #default="scope">
          <div class="tags">
            <el-tag @click="editItem(scope.row)">编辑</el-tag>
            <el-tag @click="deleteItem(scope.row._id)" type="danger">
              删除
            </el-tag>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <Pagination
      :pagination="pagination"
      :handleCurrentChange="handleCurrentChange"
      :handleSizeChange="handleSizeChange"
    />
  </el-card>

  <!-- 添加/编辑角色抽屉 -->
  <el-drawer
    v-model="drawer"
    :title="isEdit ? '编辑角色' : '添加角色'"
    direction="rtl"
  >
    <el-form :model="roleForm" label-width="80px">
      <el-form-item label="角色名称">
        <el-input v-model="roleForm.rolename" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="drawer = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import topCard from '@/components/topCard/index.vue'
import Pagination from '@/components/Pagination/index.vue'
import { formatDateTime } from '@/utils/utils'
import { addRole, getRoleList, deleteRole, updateRole ,deleteRoles} from '@/api/acl/role'

//顶部卡片
let inputName = ref('')
const getSearchInput = (input: string) => {
  inputName.value = input
  getList()
}
//重置
const reset = () => {
  inputName.value = ''
  getList()
}

//列表
let roleList = ref<any>([])
//复选框发生改变
const selectChange = (val: any) => {
  selectArr.value = val
}
let drawer = ref<boolean>(false)
let isEdit = ref(false)
let currentId = ref('')

const roleForm = reactive({
  rolename: '',
})

//获取列表数据
const getList = async () => {
  loading.value = true
  try {
    const res: any = await getRoleList(
      pagination.currentPage,
      pagination.pageSize,
      inputName.value,
    )
    console.log(res, '获取角色列表res')
    if (res.code === 200) {
      roleList.value = res.data
      pagination.total = res.total
    }
  } catch (error) {
    console.error(error)
  }
  loading.value = false
}

//添加/编辑抽屉
const addDrawer = () => {
  isEdit.value = false
  roleForm.rolename = ''
  drawer.value = true
}

const editItem = (row: any) => {
  isEdit.value = true
  currentId.value = row._id
  roleForm.rolename = row.rolename
  drawer.value = true
}

//提交表单
const submitForm = async () => {
  try {
    if (isEdit.value) {
      const res:any = await updateRole({
        _id: currentId.value,
        rolename: roleForm.rolename,
      })
      if (res.code === 200) {
        ElMessage.success('更新成功')
        drawer.value = false
        getList()
      }
    } else {
      const res:any = await addRole({
        rolename: roleForm.rolename,
      })
      if (res.code === 200) {
        ElMessage.success('添加成功')
        drawer.value = false
        getList()
      }
    }
  } catch (error) {
    console.error(error)
  }
}

//删除操作
const deleteItem = async (id: number) => {
  try {
    const res:any = await deleteRole(id)
    if (res.code === 200) {
      ElMessage.success('删除成功')
      getList()
    }
  } catch (error) {
    console.error(error)
  }
}

//批量删除
const selectArr = ref<any[]>([])
const deleteSelectItem = async () => {
  try {
    const ids = selectArr.value.map((item) => item._id)
    const res:any = await deleteRoles(ids )
    if (res.code === 200) {
      ElMessage.success('批量删除成功')
      getList()
    }
  } catch (error) {
    console.error(error)
  }
}
let loading = ref(false)

//分页器
const pagination = reactive({
  pageSize: 3,
  currentPage: 1,
  total: 0,
})
const handleCurrentChange = async (page: number) => {
  pagination.currentPage = page
  await getList()
}
const handleSizeChange = async (limit: number) => {
  pagination.pageSize = limit
  await getList()
}

onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss">
.bottom-table {
  margin: 10px 0;
}
.tags {
  .el-tag {
    margin: 0 5px;
    cursor: pointer;
  }
}
</style>
